Skip to content

[VertexAI] Refactor the LiveSession responses #1231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

a-maurice
Copy link
Collaborator

Description

Provide details of the change, and generalize the change in the PR title above.

Refactor how the LiveSession responses are handled. Rename the top level struct from LiveContentResponse to LiveSessionResponse, move the various fields into other structs that use a shared interface that LiveContentResponse now contains instead.


Testing

Describe how you've tested these changes.

Running locally.


Type of Change

Place an x the applicable box:

  • Bug fix. Add the issue # below if applicable.
  • New feature. A non-breaking change which adds functionality.
  • Other, such as a build process or documentation change.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @a-maurice, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request refactors the handling of LiveSession responses in the VertexAI API. The primary change involves renaming the top-level struct from LiveContentResponse to LiveSessionResponse and reorganizing its fields into new structs that implement a shared interface, ILiveSessionMessage. This interface is implemented by LiveSessionContent, LiveSessionToolCall, and LiveSessionToolCallCancellation, each representing different types of messages from the live session. Additionally, the PR introduces helper methods for parsing lists of strings from JSON dictionaries.

Highlights

  • Response Refactor: The top-level struct LiveContentResponse is renamed to LiveSessionResponse, and its fields are moved into new structs (LiveSessionContent, LiveSessionToolCall, LiveSessionToolCallCancellation) that implement the ILiveSessionMessage interface.
  • New String List Parsing Methods: The InternalHelpers.cs file is updated to include helper methods (TryParseStringList and ParseStringList) for parsing lists of strings from JSON dictionaries.
  • ReceiveAsync Update: The ReceiveAsync method in LiveSession.cs is updated to return an IAsyncEnumerable<LiveSessionResponse> instead of IAsyncEnumerable<LiveContentResponse>, and the logic for determining turn completion is updated to check the TurnComplete property of the LiveSessionContent message.

Changelog

Click here to see the changelog
  • vertexai/src/Internal/InternalHelpers.cs
    • Added TryParseStringList method to safely attempt converting a list of objects to a list of strings.
    • Added ParseStringList method to cast a list to a string list, throwing an exception if the cast fails.
  • vertexai/src/LiveContentResponse.cs
    • This entire file was removed as part of the refactoring.
  • vertexai/src/LiveSession.cs
    • Updated the ReceiveAsync method to return IAsyncEnumerable<LiveSessionResponse> instead of IAsyncEnumerable<LiveContentResponse>.
    • Modified the ReceiveAsync method to check response?.Message is LiveSessionContent serverContent && serverContent.TurnComplete for turn completion.
  • vertexai/src/LiveSessionResponse.cs
    • Created a new LiveSessionResponse struct to encapsulate different types of messages from the live session.
    • Introduced the ILiveSessionMessage interface and implementing structs: LiveSessionContent, LiveSessionToolCall, and LiveSessionToolCallCancellation.
    • Added logic to parse different message types from JSON responses.
    • Added properties to extract text and audio content from LiveSessionContent.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A struct's refactor,
Fields move, no longer factor,
Interface aligns.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the LiveSession responses, renaming the top-level struct and moving fields into other structs with a shared interface. The changes seem well-structured and improve the organization of the response handling. However, there are a few areas that could be improved for clarity and completeness.

Summary of Findings

  • Missing FunctionIdsToCancel implementation: The original LiveContentResponse struct had a TODO comment for FunctionIdsToCancel. This functionality seems to be missing in the refactored LiveSessionToolCallCancellation struct. It's important to ensure this feature is implemented or explicitly removed if it's no longer needed.
  • Incomplete serverContent parsing: The LiveSessionResponse.FromJson method has a TODO comment indicating that other fields within serverContent need to be parsed. This should be addressed to ensure all relevant data is being processed.
  • Potential NullReferenceException in Audio property: The Audio property in LiveSessionResponse returns null if Message is not a LiveSessionContent. This could lead to a NullReferenceException if the caller attempts to access the Audio property without checking if Message is of the correct type.

Merge Readiness

The refactoring improves the structure of the LiveSession responses. However, the missing FunctionIdsToCancel implementation, incomplete serverContent parsing, and potential NullReferenceException in the Audio property should be addressed before merging. I am unable to approve this pull request, and recommend that it not be merged until these issues are resolved. Please have others review and approve this code before merging.

.Where(part => part.MimeType == "audio/pcm")
.Select(part => part.Data.ToArray());
}
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Returning null here could lead to a NullReferenceException if the caller attempts to access the Audio property without checking if Message is of the correct type. Consider returning an empty IEnumerable<byte[]> instead.

      return Enumerable.Empty<byte[]>();

// We don't want to pass this along to the user, so return null instead.
return null;
} else if (jsonDict.TryParseValue("serverContent", out Dictionary<string, object> serverContent)) {
// TODO: Other fields

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This TODO indicates that there are other fields within serverContent that need to be parsed. Ensure that all relevant data is being processed.

// TODO: Other fields
      // Parse other fields from serverContent here

} else if (jsonDict.TryParseValue("toolCallCancellation", out Dictionary<string, object> toolCallCancellation)) {
return new LiveSessionResponse(LiveSessionToolCallCancellation.FromJson(toolCallCancellation));
} else {
// TODO: Determine if we want to log this, or just ignore it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This TODO indicates a decision needs to be made about logging or ignoring parsing failures. Determine the appropriate behavior and implement it.

// TODO: Determine if we want to log this, or just ignore it?
      // Implement logging or ignoring behavior here

@a-maurice a-maurice requested a review from cynthiajoan April 17, 2025 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants